home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1XDLAHT (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  10.6 KB  |  389 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.Action;
  4. import com.sun.java.swing.JComponent;
  5. import com.sun.java.swing.SwingUtilities;
  6. import com.sun.java.swing.Timer;
  7. import com.sun.java.swing.event.ChangeEvent;
  8. import com.sun.java.swing.event.ChangeListener;
  9. import com.sun.java.swing.event.EventListenerList;
  10. import com.sun.java.swing.plaf.TextUI;
  11. import java.awt.Component;
  12. import java.awt.Graphics;
  13. import java.awt.Point;
  14. import java.awt.Rectangle;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.FocusEvent;
  17. import java.awt.event.FocusListener;
  18. import java.awt.event.MouseEvent;
  19. import java.awt.event.MouseListener;
  20. import java.awt.event.MouseMotionListener;
  21. import java.io.IOException;
  22. import java.io.ObjectInputStream;
  23. import java.io.Serializable;
  24.  
  25. public class DefaultCaret implements Caret, Serializable, FocusListener, MouseListener, MouseMotionListener {
  26.    protected EventListenerList listenerList = new EventListenerList();
  27.    protected ChangeEvent changeEvent = null;
  28.    JTextComponent component;
  29.    boolean visible;
  30.    int dot;
  31.    int mark;
  32.    Object selectionTag;
  33.    Timer flasher;
  34.    Point magicCaretPosition;
  35.    transient UpdateHandler updateHandler = new UpdateHandler(this);
  36.    static Class class$com$sun$java$swing$event$ChangeListener;
  37.  
  38.    public void addChangeListener(ChangeListener l) {
  39.       EventListenerList var10000 = this.listenerList;
  40.       Class var10001 = class$com$sun$java$swing$event$ChangeListener;
  41.       if (var10001 == null) {
  42.          try {
  43.             var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
  44.          } catch (ClassNotFoundException var2) {
  45.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  46.          }
  47.  
  48.          class$com$sun$java$swing$event$ChangeListener = var10001;
  49.       }
  50.  
  51.       var10000.add(var10001, l);
  52.    }
  53.  
  54.    protected void adjustVisibility(Rectangle nloc) {
  55.       SwingUtilities.invokeLater(new SafeScroller(this, nloc));
  56.    }
  57.  
  58.    void changeCaretPosition(int dot) {
  59.       TextUI mapper = this.component.getUI();
  60.       Document doc = this.component.getDocument();
  61.       if (mapper != null && doc != null) {
  62.          try {
  63.             Rectangle var8 = mapper.modelToView(this.dot);
  64.             this.damage(var8);
  65.          } catch (BadLocationException var7) {
  66.             Rectangle oldLoc = null;
  67.          }
  68.  
  69.          this.dot = dot;
  70.  
  71.          Rectangle newLoc;
  72.          try {
  73.             newLoc = mapper.modelToView(this.dot);
  74.          } catch (BadLocationException var6) {
  75.             newLoc = null;
  76.          }
  77.  
  78.          if (newLoc != null) {
  79.             this.adjustVisibility(newLoc);
  80.          }
  81.  
  82.          this.damage(newLoc);
  83.          this.fireStateChanged();
  84.       }
  85.  
  86.    }
  87.  
  88.    protected void damage(Rectangle r) {
  89.       if (r != null) {
  90.          this.component.repaint(r.x - 1, r.y, 3, r.height);
  91.       }
  92.  
  93.    }
  94.  
  95.    public void deinstall(JTextComponent c) {
  96.       ((Component)c).removeMouseListener(this);
  97.       ((Component)c).removeMouseMotionListener(this);
  98.       ((Component)c).removeFocusListener(this);
  99.       ((JComponent)c).removePropertyChangeListener(this.updateHandler);
  100.       Document doc = c.getDocument();
  101.       if (doc != null) {
  102.          doc.removeDocumentListener(this.updateHandler);
  103.       }
  104.  
  105.       this.component = null;
  106.       if (this.flasher != null) {
  107.          this.flasher.stop();
  108.       }
  109.  
  110.    }
  111.  
  112.    protected void fireStateChanged() {
  113.       Object[] listeners = this.listenerList.getListenerList();
  114.  
  115.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  116.          Object var10000 = listeners[i];
  117.          Class var10001 = class$com$sun$java$swing$event$ChangeListener;
  118.          if (var10001 == null) {
  119.             try {
  120.                var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
  121.             } catch (ClassNotFoundException var3) {
  122.                throw new NoClassDefFoundError(((Throwable)var3).getMessage());
  123.             }
  124.  
  125.             class$com$sun$java$swing$event$ChangeListener = var10001;
  126.          }
  127.  
  128.          if (var10000 == var10001) {
  129.             if (this.changeEvent == null) {
  130.                this.changeEvent = new ChangeEvent(this);
  131.             }
  132.  
  133.             ((ChangeListener)listeners[i + 1]).stateChanged(this.changeEvent);
  134.          }
  135.       }
  136.  
  137.    }
  138.  
  139.    public void focusGained(FocusEvent e) {
  140.       if (this.component.isEditable()) {
  141.          this.setVisible(true);
  142.       }
  143.  
  144.    }
  145.  
  146.    public void focusLost(FocusEvent e) {
  147.       this.setVisible(false);
  148.    }
  149.  
  150.    public int getBlinkRate() {
  151.       return this.flasher == null ? 0 : this.flasher.getDelay();
  152.    }
  153.  
  154.    protected final JTextComponent getComponent() {
  155.       return this.component;
  156.    }
  157.  
  158.    public int getDot() {
  159.       return this.dot;
  160.    }
  161.  
  162.    public Point getMagicCaretPosition() {
  163.       return this.magicCaretPosition;
  164.    }
  165.  
  166.    public int getMark() {
  167.       return this.mark;
  168.    }
  169.  
  170.    protected Highlighter.HighlightPainter getSelectionPainter() {
  171.       Highlighter.HighlightPainter p = new DefaultHighlighter.DefaultHighlightPainter(this.component.getSelectionColor());
  172.       return p;
  173.    }
  174.  
  175.    public void install(JTextComponent c) {
  176.       this.component = c;
  177.       Document doc = c.getDocument();
  178.       this.dot = this.mark = 0;
  179.       if (doc != null) {
  180.          doc.addDocumentListener(this.updateHandler);
  181.       }
  182.  
  183.       ((JComponent)c).addPropertyChangeListener(this.updateHandler);
  184.       ((Component)c).addFocusListener(this);
  185.       ((Component)c).addMouseListener(this);
  186.       ((Component)c).addMouseMotionListener(this);
  187.    }
  188.  
  189.    public boolean isSelectionVisible() {
  190.       return this.selectionTag != null;
  191.    }
  192.  
  193.    public boolean isVisible() {
  194.       return this.visible;
  195.    }
  196.  
  197.    public void mouseClicked(MouseEvent e) {
  198.       if (e.getClickCount() == 2) {
  199.          Action a = new DefaultEditorKit.SelectWordAction();
  200.          a.actionPerformed((ActionEvent)null);
  201.       } else if (e.getClickCount() == 3) {
  202.          Action a = new DefaultEditorKit.SelectLineAction();
  203.          a.actionPerformed((ActionEvent)null);
  204.       }
  205.  
  206.    }
  207.  
  208.    public void mouseDragged(MouseEvent e) {
  209.       this.moveCaret(e);
  210.    }
  211.  
  212.    public void mouseEntered(MouseEvent e) {
  213.    }
  214.  
  215.    public void mouseExited(MouseEvent e) {
  216.    }
  217.  
  218.    public void mouseMoved(MouseEvent e) {
  219.    }
  220.  
  221.    public void mousePressed(MouseEvent e) {
  222.       this.positionCaret(e);
  223.       if (this.component.isEnabled()) {
  224.          this.component.requestFocus();
  225.       }
  226.  
  227.    }
  228.  
  229.    public void mouseReleased(MouseEvent e) {
  230.    }
  231.  
  232.    protected void moveCaret(MouseEvent e) {
  233.       Point pt = new Point(e.getX(), e.getY());
  234.       int pos = this.component.viewToModel(pt);
  235.       if (pos >= 0) {
  236.          this.moveDot(pos);
  237.       }
  238.  
  239.    }
  240.  
  241.    public void moveDot(int dot) {
  242.       if (dot != this.dot) {
  243.          this.changeCaretPosition(dot);
  244.          Highlighter h = this.component.getHighlighter();
  245.          int p0 = Math.min(dot, this.mark);
  246.          int p1 = Math.max(dot, this.mark);
  247.  
  248.          try {
  249.             if (this.selectionTag != null) {
  250.                h.changeHighlight(this.selectionTag, p0, p1);
  251.             } else {
  252.                Highlighter.HighlightPainter p = this.getSelectionPainter();
  253.                this.selectionTag = h.addHighlight(p0, p1, p);
  254.             }
  255.          } catch (BadLocationException var6) {
  256.             throw new StateInvariantError("Bad caret position");
  257.          }
  258.       }
  259.  
  260.    }
  261.  
  262.    public void paint(Graphics g) {
  263.       if (this.isVisible()) {
  264.          try {
  265.             TextUI mapper = this.component.getUI();
  266.             Rectangle r = mapper.modelToView(this.dot);
  267.             g.setColor(this.component.getCaretColor());
  268.             g.drawLine(r.x, r.y, r.x, r.y + r.height - 1);
  269.          } catch (BadLocationException var4) {
  270.          }
  271.       }
  272.  
  273.    }
  274.  
  275.    protected void positionCaret(MouseEvent e) {
  276.       Point pt = new Point(e.getX(), e.getY());
  277.       int pos = this.component.viewToModel(pt);
  278.       if (pos >= 0) {
  279.          this.setDot(pos);
  280.          this.setMagicCaretPosition((Point)null);
  281.       }
  282.  
  283.    }
  284.  
  285.    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  286.       s.defaultReadObject();
  287.       this.updateHandler = new UpdateHandler(this);
  288.    }
  289.  
  290.    public void removeChangeListener(ChangeListener l) {
  291.       EventListenerList var10000 = this.listenerList;
  292.       Class var10001 = class$com$sun$java$swing$event$ChangeListener;
  293.       if (var10001 == null) {
  294.          try {
  295.             var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
  296.          } catch (ClassNotFoundException var2) {
  297.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  298.          }
  299.  
  300.          class$com$sun$java$swing$event$ChangeListener = var10001;
  301.       }
  302.  
  303.       var10000.remove(var10001, l);
  304.    }
  305.  
  306.    public void setBlinkRate(int rate) {
  307.       if (rate != 0) {
  308.          if (this.flasher == null) {
  309.             this.flasher = new Timer(rate, this.updateHandler);
  310.          }
  311.  
  312.          this.flasher.setDelay(rate);
  313.       } else if (this.flasher != null) {
  314.          this.flasher.stop();
  315.          this.flasher.removeActionListener(this.updateHandler);
  316.          this.flasher = null;
  317.       }
  318.  
  319.    }
  320.  
  321.    public void setDot(int dot) {
  322.       Document doc = this.component.getDocument();
  323.       if (doc != null) {
  324.          dot = Math.min(dot, doc.getLength());
  325.       }
  326.  
  327.       dot = Math.max(dot, 0);
  328.       this.mark = dot;
  329.       if (this.dot != dot || this.selectionTag != null) {
  330.          this.changeCaretPosition(dot);
  331.       }
  332.  
  333.       if (this.selectionTag != null) {
  334.          Highlighter h = this.component.getHighlighter();
  335.          h.removeHighlight(this.selectionTag);
  336.          this.selectionTag = null;
  337.       }
  338.  
  339.    }
  340.  
  341.    public void setMagicCaretPosition(Point p) {
  342.       this.magicCaretPosition = p;
  343.    }
  344.  
  345.    public void setSelectionVisible(boolean vis) {
  346.       if (vis) {
  347.          if (this.selectionTag == null && this.dot != this.mark) {
  348.             Highlighter h = this.component.getHighlighter();
  349.             int p0 = Math.min(this.dot, this.mark);
  350.             int p1 = Math.max(this.dot, this.mark);
  351.             Highlighter.HighlightPainter p = this.getSelectionPainter();
  352.  
  353.             try {
  354.                this.selectionTag = h.addHighlight(p0, p1, p);
  355.             } catch (BadLocationException var6) {
  356.                this.selectionTag = null;
  357.             }
  358.          }
  359.       } else if (this.selectionTag != null) {
  360.          Highlighter h = this.component.getHighlighter();
  361.          h.removeHighlight(this.selectionTag);
  362.          this.selectionTag = null;
  363.       }
  364.  
  365.    }
  366.  
  367.    public void setVisible(boolean e) {
  368.       TextUI mapper = this.component.getUI();
  369.       Document doc = this.component.getDocument();
  370.       if (this.visible != e && doc != null && mapper != null) {
  371.          try {
  372.             Rectangle loc = mapper.modelToView(this.dot);
  373.             this.damage(loc);
  374.          } catch (BadLocationException var5) {
  375.          }
  376.       }
  377.  
  378.       this.visible = e;
  379.       if (this.flasher != null) {
  380.          if (this.visible) {
  381.             this.flasher.start();
  382.          } else {
  383.             this.flasher.stop();
  384.          }
  385.       }
  386.  
  387.    }
  388. }
  389.